﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;
using System.Data.OleDb;

/// <summary>
/// WebFunction 的摘要说明
/// </summary>
public class WebFunction
{
    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();

    public WebFunction()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary> 
    /// 打开数据库连接 
    /// </summary> 
    private static void openConnection()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["dcweb"].ConnectionString;
            comm.Connection = conn;
            try
            {
                conn.Open();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
    }
    /// <summary> 
    /// 关闭当前数据库连接 
    /// </summary> 
    private static void closeConnection()
    {
        if (conn.State == ConnectionState.Open)
            conn.Close();
        conn.Dispose();
        comm.Dispose();
    }
    /// <summary> 
    /// 执行Sql查询语句 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    public static void ExecuteSql(string sqlstr)
    {
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }

    /// <summary> 
    /// 执行Sql更新语句 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param>
    /// <returns>布尔值</returns>
    public static bool ExecuteUpdate(string sqlstr)
    {
        int isUpdateOk = 0;
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;

            isUpdateOk = Convert.ToInt32(comm.ExecuteNonQuery());
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        if (isUpdateOk > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary> 
    /// 执行Sql查询语句并返回第一行的第一条记录,返回值为object 使用时需要拆箱操作 -> Unbox 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <returns>object 返回值 </returns> 
    public static object ExecuteScalar(string sqlstr)
    {
        object obj = new object();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            obj = comm.ExecuteScalar();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return obj;
    }

    /// <summary> 
    /// 返回指定Sql语句的SqlDataReader，请注意，在使用后请关闭本对象，同时将自动调用closeConnection()来关闭数据库连接 
    /// 方法关闭数据库连接 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <returns>SqlDataReader对象</returns> 
    public static OleDbDataReader dataReader(string sqlstr)
    {
        OleDbDataReader dr = null;

        openConnection();
        comm.CommandText = sqlstr;
        comm.CommandType = CommandType.Text;
        dr = comm.ExecuteReader(CommandBehavior.CloseConnection);

        return dr;
    }

    ///<summary>
    ///关闭datareader
    ///传入SqlDataReader的ref
    ///</summary>
    public static void closeDataReader(ref OleDbDataReader sdr)
    {
        try
        {
            sdr.Close();
            sdr.Dispose();
        }
        catch (Exception)
        { }
    }

    /// <summary> 
    /// 返回指定Sql语句的DataSet 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <returns>DataSet</returns> 
    public static DataSet dataSet(string sqlstr)
    {
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return ds;
    }

    /// <summary> 
    /// 返回指定Sql语句的DataSet 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <param name="ds">传入的引用DataSet对象</param> 
    public static void dataSet(string sqlstr, ref DataSet ds)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }

        finally
        {
            closeConnection();
        }
    }
    /// <summary> 
    /// 返回指定Sql语句的DataTable 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <returns>DataTable</returns>
    public static DataTable dataTable(string sqlstr)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataTable datatable = new DataTable();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(datatable);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return datatable;
    }

    /// <summary> 
    /// 执行指定Sql语句,同时给传入DataTable进行赋值 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <param name="dt">ref DataTable dt </param> 
    public static void dataTable(string sqlstr, ref DataTable dt)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(dt);
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }

    /// <summary> 
    /// 执行指定Sql语句 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    public static DataView dataView(string sqlstr)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataView dv = new DataView();
        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            dv = ds.Tables[0].DefaultView;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return dv;
    }

    /// <summary> 
    /// 执行指定Sql语句,开始记录位置,和返回记录数 
    /// </summary> 
    /// <param name="sqlstr">传入的Sql语句</param> 
    /// <param name="StartIndex">开始记录位置</param>
    /// <param name="pageSize">返回记录数</param> 
    public static DataView dataView(string sqlstr, int StartIndex, int pageSize)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        DataView dv = new DataView();
        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds, StartIndex, pageSize, "pagelist");
            dv = ds.Tables["pagelist"].DefaultView;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return dv;
    }
    //绑定dropdownlist控件
    public static void dropdownlist(string textfield, string valuefield, string sqlstr, DropDownList dropdownlist)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();

        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            dropdownlist.DataSource = ds.Tables[0].DefaultView;
            dropdownlist.DataTextField = textfield;
            dropdownlist.DataValueField = valuefield;
            dropdownlist.DataBind();
            ds.Clear();
            ds.Dispose();
            //dropdownlist.Items.Insert(0,"");
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }

    }
    //绑定listbox控件
    public static void listbox(string textfield, string valuefield, string sqlstr, ListBox listbox)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();

        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            listbox.DataSource = ds.Tables[0].DefaultView;
            listbox.DataTextField = textfield;
            listbox.DataValueField = valuefield;
            listbox.DataBind();
            ds.Clear();
            ds.Dispose();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }

    }
    //绑定gridview控件
    public static void gridview(string sqlstr, GridView gridview)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();

        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            gridview.DataSource = ds.Tables[0].DefaultView;
            gridview.DataBind();
            ds.Clear();
            ds.Dispose();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }
    //绑定radiobuttonlist控件
    public static void radiobuttonlist(string textfield, string valuefield, string sqlstr, RadioButtonList rbl)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();

        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            rbl.DataSource = ds.Tables[0].DefaultView;
            rbl.DataTextField = textfield;
            rbl.DataValueField = valuefield;
            rbl.DataBind();
            ds.Clear();
            ds.Dispose();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }
    //绑定checkboxlist控件
    public static void checkboxlist(string textfield, string valuefield, string sqlstr, CheckBoxList cbl)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();

        DataSet ds = new DataSet();
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);
            cbl.DataSource = ds.Tables[0].DefaultView;
            cbl.DataTextField = textfield;
            cbl.DataValueField = valuefield;
            cbl.DataBind();
            ds.Clear();
            ds.Dispose();
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
    }
}
